import {
  Body,
  Controller,
  Delete,
  Get,
  Patch,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Menus, MenusDocument } from 'src/db/schemas/menus.schema';
import { DeleteDto } from 'src/dto/delete.dto';
import { ResponseBodyDto } from '../../dto';
import { UpdateMenuDto } from './dto/update-menus.dto';
import { CreateMenuDto } from './dto/create-menus.dto'
import { QueryMenusAllDto, QueryMenusPaginationDto } from './dto/query-menus.dto';
import { ReturnMenusAllDto, ReturnMenusPaginationDto } from './dto';
import { MenusService } from './menus.service';
import { addOne, deleteByIds, getAll, getOneById, queryPagination, toFuzzyParams, updateOneById } from 'src/utils';
import { MenuDto } from 'src/db/dto';
import { Public } from 'src/common/guards';

@ApiTags('菜单')
@Controller('menus')
@ApiBearerAuth('Authorization')
export class MenusController {
  constructor(
    private readonly menusService: MenusService,
    @InjectModel(Menus.name) private menusModel: Model<MenusDocument>,
  ) { }

  @Post('getMenusPagination')
  @ApiOkResponse({
    description: '菜单分页',
    type: ReturnMenusPaginationDto,
  })
  @ApiOperation({
    summary: '获取菜单分页',
  })
  getMenuPagination(@Body() queryMenuDto: QueryMenusPaginationDto) {
    const { pageSize, current, ...other } = queryMenuDto;
    return queryPagination(
      this.menusModel,
      toFuzzyParams(other),
      {
        pageSize,
        current,
      },
    );
  }

  @Public()
  @Post('getAllMenusByCondition')
  @ApiOkResponse({
    description: '所有菜单',
    type: ReturnMenusAllDto,
  })
  @ApiOperation({
    summary: '根据条件获取所有菜单',
  })
  getAllMenusByCondition(@Body() queryMenuDto: QueryMenusAllDto) {
    return getAll(
      this.menusModel,
      toFuzzyParams(queryMenuDto),
    );
  }

  @Put('addMenu')
  @ApiOkResponse({
    type: ResponseBodyDto
  })
  @ApiOperation({
    summary: '添加菜单',
  })
  addMenu(@Body() createMenuDto: CreateMenuDto) {
    return addOne(this.menusModel, createMenuDto)
  }

  @Put('updateMenu/:_id')
  @ApiOkResponse({
    description: '更新结果',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '更新菜单',
  })
  updateMenu(
    @Param('_id') _id: string,
    @Body() updateMenuDto: UpdateMenuDto,
  ) {
    updateOneById(this.menusModel, _id, updateMenuDto);
  }

  @Delete('deleteMenus')
  @ApiOkResponse({
    description: '删除结果',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '删除菜单',
  })
  deleteMenus(@Body() deleteDto: DeleteDto) {
    deleteByIds(this.menusModel, deleteDto.ids);
  }

  @Get('getMenuById/:_id')
  @ApiOkResponse({
    description: '菜单',
    type: MenuDto,
  })
  @ApiOperation({
    summary: '获取菜单',
  })
  getMenuById(@Param('_id') _id: string) {
    return getOneById(this.menusModel, _id);
  }
}
